package com.app.beu.core.renderer;

import java.io.IOException;

import tsm.constant.ClientConstant;
import tsm.model.Buddy;

import com.core.tsm.Component;
import com.core.tsm.Container;
import com.core.tsm.Font;
import com.core.tsm.Image;
import com.core.tsm.Label;
import com.core.tsm.List;
import com.core.tsm.layouts.BorderLayout;
import com.core.tsm.layouts.BoxLayout;
import com.core.tsm.list.ListCellRenderer;
import com.core.tsm.plaf.Style;

public class YahooBuddyRenderer extends Container implements ListCellRenderer {
	private Label icon1;
	private Label description1;
	private Label description2;
	private Container selected = new Container();

	private Label icon2;
	private Label text1;
	private Label text2;
	private Image imageCh;

	//	private String[] items;
	private Image online;
	private Image offline;

	public YahooBuddyRenderer() {
		super();
		//		try {
		//		} catch (IOException ex) {
		//			ex.printStackTrace();
		//		}

		try {
			imageCh = Image.createImage("/ch1.png");
			online = Image.createImage("/onlineyahoo.png");
			offline = Image.createImage("/offlineyahoo.png");
		} catch (IOException ex) {
		}

		icon1 = new Label();
		//		icon1.getSelectedStyle().setBackgroundType(Style.BACKGROUND_IMAGE_SCALED);
		//				icon1.getSelectedStyle().setBgImage(sem);
		icon1.getSelectedStyle().setBgColor(0x138ef6);
		icon1.getSelectedStyle().setBgTransparency(255);
		icon1.getSelectedStyle().setPadding(Component.RIGHT, 0);
		icon1.getSelectedStyle().setMargin(Component.RIGHT, 0);
		icon1.getSelectedStyle().setPadding(Component.LEFT, 0);
		icon1.getSelectedStyle().setMargin(Component.LEFT, 0);
		icon1.getSelectedStyle().setPadding(Component.BOTTOM, 0);
		icon1.getSelectedStyle().setMargin(Component.BOTTOM, 0);
		icon1.getSelectedStyle().setPadding(Component.TOP, 1);
		icon1.getSelectedStyle().setMargin(Component.TOP, 1);
		description1 = new Label(imageCh);
		//		description1 = new Label();
		//		Font font = Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE);
		//		description1.getSelectedStyle().setFont(font);
		//		description1.getSelectedStyle().setBackgroundType(Style.BACKGROUND_IMAGE_SCALED);
		//				description1.getSelectedStyle().setBgImage(h1);
		description1.getSelectedStyle().setBgColor(0x138ef6);
		description1.getSelectedStyle().setBgTransparency(255);
		description1.getSelectedStyle().setPadding(Component.LEFT, 0);
		description1.getSelectedStyle().setMargin(Component.LEFT, 0);
		description1.getSelectedStyle().setPadding(Component.RIGHT, 0);
		description1.getSelectedStyle().setMargin(Component.RIGHT, 0);
		description1.getSelectedStyle().setPadding(Component.BOTTOM, 0);
		description1.getSelectedStyle().setMargin(Component.BOTTOM, 0);
		description1.getSelectedStyle().setPadding(Component.TOP, 1);
		description1.getSelectedStyle().setMargin(Component.TOP, 1);
		description2 = new Label(imageCh);
		//		description2 = new Label();
		//		description2.getSelectedStyle().setBackgroundType(Style.BACKGROUND_IMAGE_SCALED);
		//				description2.getSelectedStyle().setBgImage(h2);
		description2.getSelectedStyle().setBgColor(0x138ef6);
		description2.getSelectedStyle().setBgTransparency(255);
		description2.getSelectedStyle().setPadding(Component.TOP, 0);
		description2.getSelectedStyle().setMargin(Component.TOP, 0);
		description2.getSelectedStyle().setPadding(Component.BOTTOM, 0);
		description2.getSelectedStyle().setMargin(Component.BOTTOM, 0);
		description2.getSelectedStyle().setPadding(Component.LEFT, 0);
		description2.getSelectedStyle().setMargin(Component.LEFT, 0);
		description2.getSelectedStyle().setPadding(Component.RIGHT, 0);
		description2.getSelectedStyle().setMargin(Component.RIGHT, 0);
		Container a = new Container(new BoxLayout(BoxLayout.Y_AXIS));
		a.addComponent(description1);
		a.addComponent(description2);
		selected.setLayout(new BorderLayout());
		selected.addComponent(BorderLayout.WEST, icon1);
		selected.addComponent(BorderLayout.CENTER, a);
		icon1.setFocus(true);
		description1.setFocus(true);
		description2.setFocus(true);
		setUIID("ListRenderer");

		setLayout(new BorderLayout());
		icon2 = new Label();
		icon2.getUnselectedStyle().setBgColor(0xd2d4d3);
		icon2.getUnselectedStyle().setBgTransparency(255);
		//		icon2.getUnselectedStyle().setPadding(0,0,0,0);
		//		icon2.getUnselectedStyle().setMargin(0,0,0,0);
		//		icon2.getUnselectedStyle().setPadding(Component.BOTTOM, 0);
		//		icon2.getUnselectedStyle().setMargin(Component.BOTTOM, 0);
		icon2.getUnselectedStyle().setPadding(Component.RIGHT, 0);
		icon2.getUnselectedStyle().setMargin(Component.RIGHT, 0);
		icon2.getUnselectedStyle().setPadding(Component.LEFT, 0);
		icon2.getUnselectedStyle().setMargin(Component.LEFT, 0);
		icon2.getUnselectedStyle().setPadding(Component.BOTTOM, 0);
		icon2.getUnselectedStyle().setMargin(Component.BOTTOM, 0);
		icon2.getUnselectedStyle().setPadding(Component.TOP, 1);
		icon2.getUnselectedStyle().setMargin(Component.TOP, 1);
		text1 = new Label(imageCh);
		//		text1 = new Label();
		text1.getUnselectedStyle().setBgColor(0xd2d4d3);
		text1.getUnselectedStyle().setFgColor(0);
		text1.getUnselectedStyle().setBgTransparency(255);
		text1.getUnselectedStyle().setPadding(Component.LEFT, 0);
		text1.getUnselectedStyle().setMargin(Component.LEFT, 0);
		text1.getUnselectedStyle().setPadding(Component.RIGHT, 0);
		text1.getUnselectedStyle().setMargin(Component.RIGHT, 0);
		text1.getUnselectedStyle().setPadding(Component.BOTTOM, 0);
		text1.getUnselectedStyle().setMargin(Component.BOTTOM, 0);
		text1.getUnselectedStyle().setPadding(Component.TOP, 1);
		text1.getUnselectedStyle().setMargin(Component.TOP, 1);
		text2 = new Label(imageCh);
		//		text2 = new Label();
		text2.getUnselectedStyle().setBgColor(0xd2d4d3);
		text2.getUnselectedStyle().setFgColor(0);
		text2.getUnselectedStyle().setBgTransparency(255);
		text2.getUnselectedStyle().setPadding(Component.TOP, 0);
		text2.getUnselectedStyle().setMargin(Component.TOP, 0);
		text2.getUnselectedStyle().setPadding(Component.BOTTOM, 0);
		text2.getUnselectedStyle().setMargin(Component.BOTTOM, 0);
		text2.getUnselectedStyle().setPadding(Component.LEFT, 0);
		text2.getUnselectedStyle().setMargin(Component.LEFT, 0);
		text2.getUnselectedStyle().setPadding(Component.RIGHT, 0);
		text2.getUnselectedStyle().setMargin(Component.RIGHT, 0);
		Container c = new Container(new BoxLayout(BoxLayout.Y_AXIS));
		c.getUnselectedStyle().setPadding(0, 0, 0, 0);
		c.getUnselectedStyle().setMargin(0, 0, 0, 0);
		c.addComponent(text1);
		c.addComponent(text2);
		addComponent(BorderLayout.WEST, icon2);
		addComponent(BorderLayout.CENTER, c);

		getUnselectedStyle().setBgTransparency(255);
		//				getUnselectedStyle().setBgImage(usm);
		//				getUnselectedStyle().setBgColor(0xd2d4d3);
		//		getUnselectedStyle().setBackgroundType(Style.BACKGROUND_IMAGE_SCALED);
		//selected.getStyle().setBgTransparency(0xFF);
	}

	public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected) {
		Buddy buddy = (Buddy) value;
		if (isSelected) {
			if (buddy.getStatus() == ClientConstant.ONLINE) {
				icon1.setIcon(online);
			} else {
				icon1.setIcon(offline);
			}
			description1.setText(buddy.getUsername());
			if (buddy.getStatusText() != null) {
				description2.setText(buddy.getStatusText());
			} else if (buddy.getFullName() != null) {
				description2.setText(buddy.getFullName());
			}
			return selected;
		}
		if (buddy.getStatus() == ClientConstant.ONLINE) {
			icon2.setIcon(online);
		} else {
			icon2.setIcon(offline);
		}
		text1.setText(buddy.getUsername());
		if (buddy.getStatusText() != null) {
			text2.setText(buddy.getStatusText());
		} else if (buddy.getFullName() != null) {
			text2.setText(buddy.getFullName());
		}
		//		setFocus(true);
		return this;
	}

	public Component getListFocusComponent(List list) {
		text1.setText("");
		text2.setText("");
		icon2.setIcon(null);
		getUnselectedStyle().setBgTransparency(0);
		return this;
	}

	//	public void setItems(String[] items) {
	//		this.items = items;
	//	}

}
